package datas.gameflow;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.color.Color;

import android.util.Log;

import datas.gameflow.ui.Point;
import datas.main.GameCenterBase;

public class MainScreen extends Scene {
	private ITextureRegion backGround;
	private ITextureRegion hline;
	private ITextureRegion vline;
	private ITextureRegion hlineBoard;
	private ITextureRegion vlineBoard;
	private ITextureRegion cell;
	private ITextureRegion circle;
	
	private BitmapTextureAtlas bgBitmapTextureAtlas;
	private BitmapTextureAtlas hlineBitmapTextureAtlas;
	private BitmapTextureAtlas vlineBitmapTextureAtlas;
	private BitmapTextureAtlas hlineBoardBitmapTextureAtlas;
	private BitmapTextureAtlas vlineBoardBitmapTextureAtlas;
	private BitmapTextureAtlas cellBitmapTextureAtlas;
	private BitmapTextureAtlas circleBitmapTextureAtlas;
	
	Sprite vlineBoardSprite1[];
	Sprite vlineBoardSprite2[];
	
	Sprite hlineBoardSprite[];
	
	Sprite circleSprite[];
	Point rectangleSprite[];

	Point temp=null;
	int vitri =0;
	
	public MainScreen(){
		super();
		
		setBackground(new Background(0.08904f,0.6274f,0.8784f));
		initScreen();
		initSprite();
	}
	
	private void initScreen(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		bgBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		backGround = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bgBitmapTextureAtlas, GameCenterBase.getInstance().engine, "index1.png",0,0);
		bgBitmapTextureAtlas.load();
		
		hlineBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		hline = BitmapTextureAtlasTextureRegionFactory.createFromAsset(hlineBitmapTextureAtlas, GameCenterBase.getInstance().engine, "hline.png",0,0);
		hlineBitmapTextureAtlas.load();
		
		hlineBoardBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		hlineBoard = BitmapTextureAtlasTextureRegionFactory.createFromAsset(hlineBoardBitmapTextureAtlas, GameCenterBase.getInstance().engine, "hlineBoard.png",0,0);
		hlineBoardBitmapTextureAtlas.load();
		
		vlineBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		vline = BitmapTextureAtlasTextureRegionFactory.createFromAsset(vlineBitmapTextureAtlas, GameCenterBase.getInstance().engine, "vline.png",0,0);
		vlineBitmapTextureAtlas.load();
		
		vlineBoardBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		vlineBoard = BitmapTextureAtlasTextureRegionFactory.createFromAsset(vlineBoardBitmapTextureAtlas, GameCenterBase.getInstance().engine, "vlineBoard.png",0,0);
		vlineBoardBitmapTextureAtlas.load();
		
		cellBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		cell = BitmapTextureAtlasTextureRegionFactory.createFromAsset(cellBitmapTextureAtlas, GameCenterBase.getInstance().engine, "index1.png",0,0);
		cellBitmapTextureAtlas.load();
		
		circleBitmapTextureAtlas = new BitmapTextureAtlas(GameCenterBase.getInstance().engine.getTextureManager(),GameCenterBase.getInstance().CAMERA_WIDTH , GameCenterBase.getInstance().CAMERA_HEIGHT);
		circle = BitmapTextureAtlasTextureRegionFactory.createFromAsset(cellBitmapTextureAtlas, GameCenterBase.getInstance().engine, "circle_edit.png",0,0);
		circleBitmapTextureAtlas.load();
		
		//Resources res = MainActivity.GameCenterBase.getInstance().engine.getResources();
		//vlineBitmapTouch = BitmapFactory.decodeResource(res, R.drawable.vline);
	}
	
	private void initSprite(){
		Sprite bgSprite = new Sprite(0, 0, backGround, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
		bgSprite.setHeight(GameCenterBase.getInstance().CAMERA_HEIGHT);
		bgSprite.setWidth(GameCenterBase.getInstance().CAMERA_WIDTH);
        this.attachChild(bgSprite);
        
        //create layer
        this.attachChild(new Entity());//1
        this.attachChild(new Entity());//2
        this.attachChild(new Entity());//3
        this.attachChild(new Entity());//4
        
		//draw frame
		float width = GameCenterBase.getInstance().CAMERA_WIDTH;
		float height = GameCenterBase.getInstance().CAMERA_HEIGHT;
		
		float height3 = hline.getHeight()*30/100;
		final float width3 = height3;
		
		//new top, bottom, right,left
		float newHeight = height - vline.getWidth()*2;
		
		//size cell
		final float widthCell = (newHeight - width3*5)/4;
		
		//board
		float boardWidth = widthCell*4 + width3*5 + vline.getWidth()*2;
		float offset = width - boardWidth;
		final float leftBoard = offset/2;
		//number imageVline
		float numVline = height/vline.getHeight();
		int imageVlineNum = (int)numVline;
		if((numVline-imageVlineNum)>0) imageVlineNum+=1;
		
		//number imageVline
		float numHline = width/hline.getWidth();
		int imageHlineNum = (int)numHline;
		if((numHline-imageHlineNum)>0) imageHlineNum+=1;
		
		//draw board
		//draw vertical line
		int i;
		int h;
		vlineBoardSprite1 = new Sprite[imageVlineNum*5];
		vlineBoardSprite2 = new Sprite[imageVlineNum*5];
		for(i=0;i<5;i++){
			for(h=0;h<imageVlineNum;h++){
				
				vlineBoardSprite1[i*imageVlineNum+h] = new Sprite((leftBoard+vline.getWidth())+(widthCell+width3)*i, h*vline.getHeight(),width3*2,vline.getHeight(),vlineBoard,GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
				this.getChildByIndex(2).attachChild(vlineBoardSprite1[i*imageVlineNum+h]);
				this.registerTouchArea(vlineBoardSprite1[i*imageVlineNum+h]);
				
				//vlineBoardSprite2[i*imageVlineNum+h] = new Sprite((leftBoard+vline.getWidth())+(widthCell+width3)*i, h*vline.getHeight(),width3*2,vline.getHeight(),vline,GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
				//this.getChildByIndex(1).attachChild(vlineBoardSprite2[i*imageVlineNum+h]);
			}
		}
		//draw horizontal line
		/*
		int j,w;
		hlineBoardSprite = new Sprite[imageVlineNum*5];
		for(j=0;j<5;j++){
			for(w=0;w<imageVlineNum;w++){
				hlineBoardSprite[j*imageVlineNum+w] = new Sprite(w*hline.getWidth()+leftBoard, hline.getHeight() + (widthCell+width3)*j, hlineBoard, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
				hlineBoardSprite[j*imageVlineNum+w].setSize(hline.getWidth(), width3);
				this.attachChild(hlineBoardSprite[j*imageVlineNum+w]);
			}
		}*/
		//end draw board
		
		//draw background
		//draw horizontal line
		Sprite hlineSprite[] = new Sprite[imageHlineNum*2];
		for(int w1=0;w1<imageHlineNum;w1++){
			hlineSprite[w1*2] = new Sprite(w1*hline.getWidth(), 0, hline, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
			this.attachChild(hlineSprite[w1*2]);
			
			hlineSprite[w1*2+1] = new Sprite(w1*hline.getWidth(), (height-hline.getHeight()), hline, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
			this.attachChild(hlineSprite[w1*2+1]);
		}
		
		//draw vertical line
		Sprite vlineSprite[] = new Sprite[imageVlineNum*2];
		for(int h1=0;h1<imageVlineNum;h1++){
			vlineSprite[h1*2] = new Sprite(leftBoard, h1*vline.getHeight(), vline, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
			this.attachChild(vlineSprite[h1*2]);
			
			vlineSprite[h1*2+1] = new Sprite(leftBoard+boardWidth-vline.getWidth(), h1*vline.getHeight(), vline, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
			this.attachChild(vlineSprite[h1*2+1]);
		}				
		
		//draw circle
		circleSprite = new Sprite[25];
		for(int row=0;row<5;row++){
			for(int col=0;col<5;col++){
				circleSprite[col*5 + row] = new Sprite((leftBoard+vline.getWidth())+(widthCell+width3)*col, hline.getHeight() + (widthCell+width3)*row, width3*4, width3*4, circle, GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
				this.attachChild(circleSprite[col*5+row]);
				this.registerTouchArea(circleSprite[col*5+row]);
			}
		}
		
		//draw rectangle
		//draw circle
		rectangleSprite = new Point[25];
		for(int row=0;row<5;row++){
			for(int col=0;col<5;col++){
				rectangleSprite[col*5 + row] = new Point((leftBoard+vline.getWidth())+(widthCell+width3)*col, hline.getHeight() + (widthCell+width3)*row, width3*25, width3*25, circle, GameCenterBase.getInstance().engine.getVertexBufferObjectManager()){
					@Override
					public boolean onAreaTouched(final TouchEvent pthisTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
						if(pthisTouchEvent.isActionDown()){
							temp = this;
							Log.d("andengine", "down");
						}
						if(pthisTouchEvent.isActionUp()){
							if(temp!=null){
							float x,y,width1,height1;
							if((temp.getX()==getX()) && (temp.getY()!=getY())){
								x = getX();
								if(temp.getY()>getY()){
									y = getY();
									height1 = vline.getHeight();
									width1 = temp.getY() -getY();
									
									vlineBoardSprite2[vitri] = new Sprite(x,y,width1,height1,vline,GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
									this.attachChild(vlineBoardSprite2[vitri]);
									vitri+=1;
								}
								if(temp.getY()<getY()){
									y = temp.getY();
									height1 = vline.getHeight();
									width1 = getY() - temp.getY();
									
									vlineBoardSprite2[vitri] = new Sprite(x,y,width1,height1,vline,GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
									this.attachChild(vlineBoardSprite2[vitri]);
									vitri+=1;
								}
							}
							
							if((temp.getY()==getY()) && (temp.getX()!=getX())){
								y = getY();
								if(temp.getX()>getX()){
									x = getY();
									height1 = temp.getX()-getX();
									width1 = hline.getWidth();
									
									vlineBoardSprite2[vitri] = new Sprite(x, y,width1,height1,hline,GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
									this.attachChild(vlineBoardSprite2[vitri]);
									vitri+=1;
								}
								if(temp.getX()<getX()){
									x = temp.getX();
									height1 = getX() - temp.getX();
									width1 = hline.getWidth();
									
									vlineBoardSprite2[vitri] = new Sprite(x, y,width1,height1,hline,GameCenterBase.getInstance().engine.getVertexBufferObjectManager());
									this.attachChild(vlineBoardSprite2[vitri]);
									vitri+=1;
								}

							}
							}
							Log.d("andengine", "up");
						}
						return false;
                      }
				};
				rectangleSprite[col*5+row].setAlpha(0);
				this.attachChild(rectangleSprite[col*5+row]);
				this.registerTouchArea(rectangleSprite[col*5+row]);
			}
		}
	}
	
	
}
